package com.ruoyi.app.controller.business.audit;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.business.audit.factory.JbAuditFactory;
import com.ruoyi.web.controller.business.order.factory.JbApprovalLogFactory;
import com.ruoyi.web.controller.business.order.factory.JbOrderFactory;
import com.ruoyi.web.controller.business.order.factory.JbPaymentReceivedFactory;
import com.ruoyi.web.controller.business.order.factory.JbRefundFactory;
import com.ruoyi.web.controller.business.order.vo.JbAuditVO;
import com.ruoyi.web.controller.business.order.vo.JbOrderVO;
import com.ruoyi.web.controller.flyflow.factory.FlyFlowFactory;
import com.ruoyi.web.controller.flyflow.vo.FlyFlowTaskListVO;
import com.ruoyi.web.controller.tool.JoCodeUtils;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 审批 控制类
 *
 * @author Jyl
 */
@RestController
@RequestMapping("/app/audit")
public class AppAuditController extends JoController
{
    @Autowired
    private JbAuditFactory jbAuditFactory;
    @Autowired
    private JbApprovalLogFactory jbApprovalLogFactory;
    @Autowired
    private FlyFlowFactory flyFlowFactory;
    @Autowired
    private JbOrderFactory jbOrderFactory;
    @Autowired
    private JbRefundFactory jbRefundFactory;
    @Autowired
    private JbPaymentReceivedFactory jbPaymentReceivedFactory;

    /**
     * 通用审批
     */
    @ApiOperation("通用审批")
    @PostMapping()
    public AjaxResult audit(@RequestBody JbAuditVO jbAuditVO)
    {
        return JoR.success(jbAuditFactory.audit(jbAuditVO));
    }

    /**
     * 撤销审批
     */
    @ApiOperation("撤销审批")
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestBody JbOrderVO jbOrderVO)
    {
        return JoR.success(jbAuditFactory.revoke(jbOrderVO.getId()));
    }

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @GetMapping("/orderList")
    public AjaxResult orderList(PageParam param, String auditStatus)
    {
        return JoR.success(jbAuditFactory.orderTask(param));
    }

    /**
     * 查询回款列表
     */
    @ApiOperation("查询回款列表")
    @GetMapping("/paymentReceivedList")
    public AjaxResult paymentReceivedList(PageParam param, String auditStatus)
    {
        return JoR.success(jbAuditFactory.paymentTask(param));
    }

    /**
     * 查询退款退货列表
     */
    @ApiOperation("查询退款退货列表")
    @GetMapping("/refundList")
    public AjaxResult refundList(PageParam param, String auditStatus)
    {
        return JoR.success(jbAuditFactory.refundTask(param));
    }

    /**
     * 查询待办
     */
    @ApiOperation("查询待办")
    @GetMapping("/querTodoTaskList")
    public AjaxResult queryTodoTaskList(String orderNumber)
    {
        if (StrUtil.isNotEmpty(orderNumber))
        {
            String tag = orderNumber.substring(0, 2);
            JSONObject jsonObject = new JSONObject();
            jsonObject = switch (tag)
            {
                case JoCodeUtils.ORDERCODE ->
                        JSONObject.parseObject(JSONObject.toJSONString(jbOrderFactory.getInfoByNumber(orderNumber)));
                case JoCodeUtils.REFUNDCODE ->
                        JSONObject.parseObject(JSONObject.toJSONString(jbRefundFactory.getInfo(orderNumber)));
                case JoCodeUtils.PAYMENTRECEIVEDCODE ->
                        JSONObject.parseObject(JSONObject.toJSONString(jbPaymentReceivedFactory.getInfo(orderNumber)));
                default -> throw new IllegalStateException("Unexpected value: " + tag);
            };

            if (jsonObject.isEmpty())
            {
                return AjaxResult.error("未查询到业务单据");
            }

            JSONObject orderStatistics = flyFlowFactory.queryTodoTaskList(FlyFlowTaskListVO.builder()
                    .processId(jsonObject.getString("processNumber"))
                    .pageNum(1).pageSize(10).build());
            JSONObject data = orderStatistics.getJSONObject("data");
            JSONArray records = data.getJSONArray("records");
            return JoR.success(jbAuditFactory.getResult(records, new HashMap(), data.getString("total")));
        }
        return AjaxResult.error("缺少单据编码");
    }
}
